<template>
  <div>
    <h2>先看源代码</h2>
  </div>
</template>

<script>
export default {
  name: "Import",
  data() {
    return {};
  },
  methods: {
    // 下载模板接口
    axiosImport() {
      return request.md({
        url: `/v1/{organizationId}/md-org-trees/template/download`,
        method: "POST",
        params: {
          treeType: "production_line",
        },
        responseType: "blob",
      });
    },
    // 调用下载模板接口
    ha() {
      exportTemplate().then((res) => {
        let b = new Blob([res], {
          type: "application/vnd.ms-excel",
        });
        let url = URL.createObjectURL(b);
        let link = document.createElement("a");
        // 导出的文件名
        let fileName = res.filename;
        link.download = fileName;
        link.href = url;
        link.click();

        this.close();
      });
    },

    // 导入接口
    importFile() {
      if (!this.form.file) {
        this.$message.warning("请上传的文件");
        return;
      }
      this.loading = true;
      this.errorExportUrl = "";
      let bodyData = new FormData();
      bodyData.append("file", this.form.file);
      bodyData.append("importEnum", 1);
      importFile({}, bodyData)
        .then((res) => {
          if (res.isSuccess) {
            this.importBeforeFlag = "success";
            this.$message.success("导入成功");
            this.close();
          } else {
            this.importBeforeFlag = "error"; //success error
            this.importErrorData = {
              total: res.totalNum,
              success: res.successNum,
              error: res.failNum,
            };
            this.errorExportUrl = res.url;
          }
          this.$emit("refresh");
        })
        .finally(() => {
          this.loading = false;
        });
    },

    // 导出
    exportData() {
      this.$refs.form
        .validate()
        .then(() => {
          if (this.form.exportType === "select" && !this.selectRows.length) {
            this.$message.warning("请选择需要导出的数据");
            return;
          }
          let bodyData = {
            fileTypeEnum: this.form.fileType,
            queryOrgTreeAssignRootDTO: {
              keyword: this.searchQuery,
              treeType: "production_line",
            },
          };
          this.loading = true;
          exportTable(bodyData)
            .then((res) => {
              let b = new Blob([res], {
                type: "application/vnd.ms-excel",
              });
              let url = URL.createObjectURL(b);
              let link = document.createElement("a");
              // 导出的文件名
              let fileName = res.filename;
              link.download = fileName;
              link.href = url;
              link.click();

              this.close();
            })
            .finally(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
  },
  // 导出接口
  imporOut() {
    function exportTable(data) {
      return request.md({
        url: `/v1/{organizationId}/md-org-trees/export`,
        method: "POST",
        data,
        responseType: "blob",
      });
    }
  },
};
</script>

<style scoped></style>
